import 'package:enterprise_v8/app/components/toast_widget.dart';
import 'package:enterprise_v8/app/core/core.dart';
import 'package:enterprise_v8/modules/home/home_page/home_page_view.dart';
import 'package:enterprise_v8/modules/home/message/message_view.dart';
import 'package:enterprise_v8/modules/home/user/user_view.dart';
import 'package:enterprise_v8/modules/home/workbench/workbench_view.dart';

class HomeController extends GetxController {
  var index = 0.obs;

  final myTabs = <Widget>[
    HomePagePage(),
    const WorkbenchPage(),
    const MessagePage(),
    UserPage(),
  ];

  final tabName = ['首页', '工作台', '消息', '我的'];

  final tabIcon = [
    Icons.home,
    Icons.work,
    Icons.message,
    Icons.person,
  ];

  /// 上一次点击返回的时间
  DateTime lastBackPressTime = DateTime.now();

  /// WillPopScope退出判断
  bool exitReminder() {
    if (DateTime.now().difference(lastBackPressTime) > const Duration(seconds: 2)) {
      AppToast.showWarning('再次点击退出');
      lastBackPressTime = DateTime.now();
      return false;
    } else {
      return true;
    }
  }

  /// 判断当前版本号和后台版本号是否一致
  /// [newVersion] 后台版本号
  /// [old] 当前版本号
  /// return true 需要升级 false 不需要升级
  bool isUpdateVersion(String? newVersion, String? old) {
    if (newVersion == null || newVersion.isEmpty || old == null || old.isEmpty) {
      return false;
    }
    int newVersionInt, oldVersion;
    final newList = newVersion.split('.');
    final oldList = old.split('.');
    if (newList.isEmpty || oldList.isEmpty) {
      return false;
    }
    for (int i = 0; i < newList.length; i++) {
      newVersionInt = int.parse(newList[i]);
      oldVersion = int.parse(oldList[i]);
      if (newVersionInt > oldVersion) {
        return true;
      } else if (newVersionInt < oldVersion) {
        return false;
      }
    }
    return false;
  }

  // 关闭页面
  closePage() {
    /// 返回带参
    Get.back();
  }
}
